package edu.ynu.dao;
// default package

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import edu.ynu.po.Customer;
import edu.ynu.po.LinkPerson;

/**
 	* A data access object (DAO) providing persistence and search support for LinkPerson entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .LinkPerson
  * @author MyEclipse Persistence Tools 
 */

public class LinkPersonDAO extends BaseHibernateDAO  {
		 private static final Log log = LogFactory.getLog(LinkPersonDAO.class);
		//property constants
	public static final String TYPE = "type";
	public static final String CUSTOMER_ID = "customerId";
	public static final String NAME = "name";
	public static final String SEX = "sex";
	public static final String EMAIL = "email";
	public static final String CREATETIME = "createtime";
	public static final String LASTTIME = "lasttime";
	public static final String RELATION = "relation";
	public static final String TITLE = "title";
	public static final String DEPARTMENT = "department";
	public static final String POSITION = "position";
	public static final String REMARK = "remark";



    
    public void save(LinkPerson transientInstance) {
        log.debug("saving LinkPerson instance");
        try {
        	Session session = getSession();
        	session.clear();
        	Transaction trans = session.beginTransaction();
            session.save(transientInstance);
            trans.commit();  
    		session.close(); 
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(LinkPerson persistentInstance) {
        log.debug("deleting LinkPerson instance");
        try {
        	Session session = getSession();
        	session.clear();
        	Transaction trans = session.beginTransaction();
            session.delete(persistentInstance);
            trans.commit();  
    		session.close(); 
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	public void update(LinkPerson persistentInstance){
		Session session = getSession();
		session.clear();
    	Transaction trans = session.beginTransaction();
        session.update(persistentInstance);
        trans.commit();  
		session.close(); 
	}
    
    public LinkPerson findById( java.lang.Integer id) {
        log.debug("getting LinkPerson instance with id: " + id);
        try {
            LinkPerson instance = (LinkPerson) getSession()
                    .get("edu.ynu.po.LinkPerson", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(LinkPerson instance) {
        log.debug("finding LinkPerson instance by example");
        try {
            List results = getSession()
                    .createCriteria("LinkPerson")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding LinkPerson instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from LinkPerson as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByType(Object type
	) {
		return findByProperty(TYPE, type
		);
	}
	
	public List findByCustomerId(Object customerId
	) {
		return findByProperty(CUSTOMER_ID, customerId
		);
	}
	
	public List findByName(Object name
	) {
		return findByProperty(NAME, name
		);
	}
	
	public List findBySex(Object sex
	) {
		return findByProperty(SEX, sex
		);
	}
	
	public List findByEmail(Object email
	) {
		return findByProperty(EMAIL, email
		);
	}
	
	public List findByCreatetime(Object createtime
	) {
		return findByProperty(CREATETIME, createtime
		);
	}
	
	public List findByLasttime(Object lasttime
	) {
		return findByProperty(LASTTIME, lasttime
		);
	}
	
	public List findByRelation(Object relation
	) {
		return findByProperty(RELATION, relation
		);
	}
	
	public List findByTitle(Object title
	) {
		return findByProperty(TITLE, title
		);
	}
	
	public List findByDepartment(Object department
	) {
		return findByProperty(DEPARTMENT, department
		);
	}
	
	public List findByPosition(Object position
	) {
		return findByProperty(POSITION, position
		);
	}
	
	public List findByRemark(Object remark
	) {
		return findByProperty(REMARK, remark
		);
	}
	

	public List findAll(Map paramMap) {
		log.debug("finding all LinkPerson instances");
		try {
			String custNo = (String) paramMap.get("custNo");
			String custName = (String)paramMap.get("custName");
			String type = (String)paramMap.get("type");
			CustomerDAO custDao = new CustomerDAO();
			System.out.println("custNo:"+custNo);
			if(StringUtils.isNotEmpty(custNo)){
				List custs = custDao.findByCustNo(custNo);
			for(int i = 0; i<custs.size(); i++){
				Customer cust = (Customer)custs.get(i);
				custName = cust.getName();
				
			}
			}
			System.out.println(custName);
			Criteria c = getSession().createCriteria(LinkPerson.class);
			if(StringUtils.isNotEmpty(custName)){
				c.add(Expression.like("custName", "%"+custName+"%"));
			}
			if(StringUtils.isNotEmpty(type)){
				c.add(Expression.like("type", "%"+type+"%"));
			}
			return c.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public LinkPerson merge(LinkPerson detachedInstance) {
        log.debug("merging LinkPerson instance");
        try {
            LinkPerson result = (LinkPerson) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(LinkPerson instance) {
        log.debug("attaching dirty LinkPerson instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(LinkPerson instance) {
        log.debug("attaching clean LinkPerson instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}